#!/usr/bin/env python
#
# Copyright 2009 Acumino - http://www.acumino.com/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Example usage:
>>> from config import config
>>> config.test
{'menus': [{'url': 'http://www.acumino.com/', 'main': 'Main Menu'}], \
'version': 1, 'license': 'Apache License, Version 2.0', 'author': 'Acumino'}
"""
import os, yaml

class Config(object):
	"""Loads all .yaml files from the 'config.d' directory into attributes"""
	def __init__(self):
		self.config_d = ''
		self.config_d = os.path.dirname(__file__) + '/../config.d'
		self.config_files = os.listdir(self.config_d)
		for config_file in self.config_files:
			(basename, extension) = config_file.split('.')
			if extension == 'yaml':
				c = yaml.load(open(self.config_d + '/' + config_file, 'r').read())
				setattr(self, basename, c)
	
	def dump(self, basename):
		"""Dumps the configuration 'c' in YAML format"""
		c = getattr(self, basename)
		return yaml.dump(c)

global config
config = Config()
